<template>
	<zui-box>
		<!-- 头部导航 -->
		<zui-navbar title="设置"></zui-navbar>

		<u-gap height="30rpx"></u-gap>

		<view class="cell_avatar" @click="xz_go('set_user')">
			<u-cell-group :clickable="true" :border="false">
				<u-cell title=" " isLink :border="false">
					<view class="avatar_box" slot="icon"><zui-avatar :src="info.avatar" size="100rpx"></zui-avatar></view>
					<view slot="title" class="title">{{ info.nickname }}</view>
				</u-cell>
			</u-cell-group>
		</view>

		<u-gap height="20rpx"></u-gap>

		<view class="cell_list">
			<u-cell-group :border="false">
				<u-cell size="large" title="账号" :value="info.mobile"></u-cell>
				<u-cell v-if="info.parent" :border="false" size="large" title="我的推荐人" :value="info.parent.mobile"></u-cell>
				<u-cell v-else :border="false" size="large" title="绑定上级" @click="xz_go('set_parent')" :isLink="true"></u-cell>
			
				
			</u-cell-group>
		</view>
		
		<!-- app版本管理 -->
		<!-- #ifdef APP-PLUS-->
		<u-gap height="20rpx"></u-gap>
		<view class="cell_list">
			<u-cell-group :border="false">
				<u-cell :border="false" size="large" title="版本" :value="version.new_version"></u-cell>
			</u-cell-group>
		</view>
		<!-- #endif -->
		

		<u-gap height="20rpx"></u-gap>

		<view class="cell_list">
			<u-cell-group :border="false">
				<u-cell :border="false" title="安全中心" isLink @click="xz_go('set_safe')"></u-cell>
				<u-cell :border="false" title="收款方式" isLink @click="xz_go('pay_list')"></u-cell>
				<u-cell
					:border="false"
					title="申请代理"
					isLink
					v-if="info.agent_state != 1 && agent == 1"
					@click="navpage()"
					:value="info.agent_state == 2 ? '去申请' : info.agent_state == 3 ? '待审核' : info.agent_state == 4 ? '已驳回' : ''"
				></u-cell>
				<u-cell :border="false" title="驳回原因" v-if="info.agent_state == 4" :label="info.agent_audit_reason"></u-cell>
			</u-cell-group>
		</view>
		<u-gap height="198rpx"></u-gap>
		<zui-button bgColor="#AA334F" @fun="loginout">退出登录</zui-button>
		<view class="link_btn" @click="xz_go('cut_user')"><view class="text">切换账号</view></view>
	</zui-box>
</template>

<script>
export default {
	data() {
		return {
			version:'',
			info: {},
			agent: 0 //是否有申请代理的资格 1-是 0-否
		};
	},
	onShow() {
		this.get_info();
		//获取版本号
		this.get_version();
		
		
		
	},
	methods: {
		//获取版本
		get_version(){
			
			// #ifdef APP-PLUS
			let that = this;
			plus.runtime.getProperty(plus.runtime.appid, function(widgetInfo) {
				let version = widgetInfo.version;
				// that.type = version;
				// let url;
				// if (that.tourist) {
				// 	url = '/app/auth/version';
				// } else {
				// 	url = '/app/user/version';
				// }
				// that.$ajax(url, 'get', {
				// 	type: 1
				// }).then(res => {
				// 	console.log('版本信息', res);
				// 	if (res.code == 1) {
				// 		if (version != res.new_version) {
				// 			that.edition(res.data.url);
				// 		}
				// 	}
				// });
				
				uni.$u.http
					.get('/app/version/getVersion', {
						params: {
							sys_version:3,
						},
						custom: {
							auth: true,
							toast: true,
							catch: true
						}
					})
					.then(res => {
						that.version = res;
						if (version != res.new_version) {
							that.edition(res.download_url);
						}
					})
					.catch(err => {});
					
					
			});
			// #endif
			
			
		
		},
		
		
		//安装更新
		edition(url) {
			console.log('当前版本号', url);
			uni.showLoading({
				title: '已有新版本,升级中...',
				mask: true
			});
			const downloadTask = uni.downloadFile({
				url: url,
				success: downloadResult => {
					if (downloadResult.statusCode === 200) {
						plus.runtime.install(
							downloadResult.tempFilePath,
							{},
							function() {
								plus.nativeUI.closeWaiting();
								console.log('安装wgt文件成功！');
								plus.nativeUI.alert('应用资源更新完成！', function() {
									plus.runtime.restart();
								});
								uni.hideLoading();
							},
							function(e) {
								plus.nativeUI.closeWaiting();
								console.log('安装wgt文件失败[' + e.code + ']：' + e.message);
								plus.nativeUI.alert('安装wgt文件失败[' + e.code + ']：' + e.message);
								uni.hideLoading();
							}
						);
					} else {
						plus.nativeUI.alert('安装wgt文件失败');
					}
				},
				fail: err => {
					plus.nativeUI.alert('下载wgt文件失败');
				}
			});
			downloadTask.onProgressUpdate(function(res) {
				console.log('下载进度' + res.progress);
				console.log('已经下载的数据长度' + res.totalBytesWritten);
				console.log('预期需要下载的数据总长度' + res.totalBytesExpectedToWrite);
			});
		},
		get_info() {
			uni.$u.http
				.get('/app/user/getUserInfo', {
					custom: {
						auth: true,
						toast: true,
						catch: true
					}
				})
				.then(res => {
					this.info = res;
				})
				.catch(err => {});
			uni.$u.http
				.get('/app/user/getAgentQualification', {
					custom: {
						auth: true,
						toast: true,
						catch: true
					}
				})
				.then(res => {
					this.agent = res.qualification_state;
				})
				.catch(err => {});
		},
		navpage() {
			if (this.info.agent_state == 2 || this.info.agent_state == 4) {
				uni.navigateTo({
					url: './agent'
				});
			}
		},
		loginout() {
			uni.showModal({
				title: '提示',
				content: '确定要退出登录吗?',
				success: function(res) {
					if (res.confirm) {
						console.log('用户点击确定');
						uni.removeStorageSync('token');
						setTimeout(() => {
							uni.reLaunch({
								url: '../login/login'
							});
						}, 10);
					} else if (res.cancel) {
						console.log('用户点击取消');
					}
				}
			});
		}
	}
};
</script>

<style lang="scss" scoped>
.title {
	color: #999;
}

.avatar_box {
	margin-right: 30rpx;
}

/* 或者单是设置透明度 */
/* //底部按钮组 */
.cell_avatar {
	background: #fff;

	/deep/.u-cell__body {
		padding: 40rpx !important;
	}

	/deep/.u-cell__title-text {
		color: #999 !important;
		font-size: 28rpx;
	}

	/deep/.u-cell__left-icon-wrap {
		margin-right: 20rpx;
	}

	/deep/.u-cell--clickable {
		background: #232539;
	}

	/deep/.u-cell {
		background: #fff;
		margin-bottom: 20rpx;
		border-radius: 20rpx;
	}
}

.cell_list {
	background: #fff;

	/deep/.u-cell__title-text {
		color: #999 !important;
		font-size: 28rpx;
	}

	/deep/.u-cell--clickable {
		background: #fff;
	}

	/deep/.u-cell {
		background: #fff;
	}
	.line {
		width: 730rpx;
		margin: 0 auto;
	}
	/deep/.u-cell__value {
		font-size: 28rpx;
		color: #999 !important;
	}
}

//链接按钮
.link_btn {
	display: flex;
	justify-content: center;

	.text {
		border-radius: 10rpx;
		font-size: 30rpx;
		color: #999;
		padding: 30rpx;
		display: inline-block;
	}
}
</style>
